/*
 * imu.h
 *
 *  Created on: Apr 23, 2013
 *      Author: Daniel
 */

#ifndef IMU_H_
#define IMU_H_

#include "DSP28x_Project.h"

typedef char byte;

#define IMU_I2C_Address			0x68	// slave address of IMU

// register map

#define IMU_ADDR_WHO_AM_I		0x00	// read for slave address

// the offsets below are signed integers
#define IMU_ADDR_X_OFFS_USRH	0x0C	// upper byte of the X offset
#define IMU_INIT_X_OFFS_USRH	0xCB

#define IMU_ADDR_X_OFFS_USRL	0x0D	// lower byte of the X offset
#define IMU_INIT_X_OFFS_USRL	0xA9

#define IMU_ADDR_Y_OFFS_USRH	0x0E	// upper byte of the Y offset
#define IMU_INIT_Y_OFFS_USRH	0x87

#define IMU_ADDR_Y_OFFS_USRL	0x0F	// lower byte of the Y offset
#define IMU_INIT_Y_OFFS_URSL	0x65

#define IMU_ADDR_Z_OFFS_USRH	0x10	// upper byte of the Z offset
#define IMU_INIT_Z_OFFS_USRH	0x43

#define IMU_ADDR_Z_OFFS_USRL	0x11	// lower byte of the Z offset
#define IMU_INIT_Z_OFFS_USRL	0x21

#define IMU_NUMBYTES_OFFSETS	7		// 1 address + 6 data
#define IMU_NUMBYTES_POLL_OS	1		// 1 address
#define IMU_NUMBYTES_READ_OS	6		// 6 data

#define IMU_ADDR_SMPLRT_DIV		0x15	// divides the sample rate by SMPLRT_DIV + 1
#define IMU_INIT_SMPLRT_DIV		0x00
#define IMU_NUMBYTES_SMPLRT_DIV	2		// 1 address + 1 data

#define IMU_ADDR_DLPF_FS		0x16	// decides the full-scale range and digital low-pass filter
#define IMU_INIT_DLPF_FS		0 | 3 << 2 | 3 << 0
#define IMU_NUMBYTES_DLPF_FS	2
//	X << 3	Full-Scale Range selection
//	X	|	Full-Scale Range
//	0		+/- 250 dps
//	1		+/- 500 dps
//	2		+/- 1000 dps
//	3		+/- 2000 dps
//
//	X << 0	Digital Low-Pass Filter selection
//	X	|	Bandwidth	|	Sample Rate
//	0		256Hz			8kHz
//	1		188Hz			1kHz
//	2		98Hz			1kHz
//	3		42Hz			1kHz
//	4		20Hz			1kHz
//	5		10Hz			1kHz
//	6		5Hz				1kHz
//	7		Rsvd			Rsvd

#define IMU_ADDR_AUX_BURST_ADDR	0x18
#define IMU_INIT_AUX_BURST_ADDR	0x32	// from accelerometer datasheet
#define IMU_NUMBYTES_AUX_BURST_ADDR 2	// 1 address + 1 data

#define IMU_ADDR_TEMP_OUT_H		0x1B
#define IMU_ADDR_TEMP_OUT_L		0x1C
#define IMU_ADDR_GYRO_XOUT_H	0x1D
#define IMU_ADDR_GYRO_XOUT_L	0x1E
#define IMU_ADDR_GYRO_YOUT_H	0x1F
#define IMU_ADDR_GYRO_YOUT_L	0x20
#define IMU_ADDR_GYRO_ZOUT_H	0x21
#define IMU_ADDR_GYRO_ZOUT_L	0x22
#define IMU_ADDR_AUX_XOUT_H		0x23
#define IMU_ADDR_AUX_XOUT_L		0x24
#define IMU_ADDR_AUX_YOUT_H		0x25
#define IMU_ADDR_AUX_YOUT_L		0x26
#define IMU_ADDR_AUX_ZOUT_H		0x27
#define IMU_ADDR_AUX_ZOUT_L		0x28

#define IMU_NUMBYTES_POLL		1		// 1 address
#define IMU_NUMBYTES_READ_ALL	14		// 14 data

void InitIMU(void);
void InitIMU_Offsets(void);
void InitIMU_SMPLRT_DIV(void);
void InitIMU_DLPF_FS(void);
void InitIMU_AUX_BURST_ADDR(void);
void IMU_read_offsets(Uint16 results[]);
void IMU_read_all(Uint16 * results);

#endif /* IMU_H_ */
